package zillow.api.results;

import org.w3c.dom.*;
import org.xml.sax.*;

import javax.xml.parsers.*;
import java.io.*;

public class walkscore {
		String result;
		private static final DocumentBuilderFactory dbFac;
	    private static final DocumentBuilder docBuilder;
	    static
	    {
	        try
	        {
	            dbFac = DocumentBuilderFactory.newInstance();
	            docBuilder = dbFac.newDocumentBuilder();
	        }
	        catch(ParserConfigurationException e)
	        {
	            throw new RuntimeException(e);
	        }
	    }
	    private static final String WS_URL = "http://api.walkscore.com/score?format=xml";
	    private static final String WSID = "e6b9ab999aecb4b879793f684a751825";
		private static String walkscoreT = null;
		private static String wsdesT = null;
	    
		public String getWalkScore(String address, String lat, String lon) throws SAXException, IOException
		{
			Document wsDoc = docBuilder.parse(WS_URL + 
                    "&address=" + address + 
                    "&lat=" + lat +
                    "&lon=" + lon +
                    "&wsapikey=" + WSID);
			Element status = (Element)wsDoc.getElementsByTagName("status").item(0);
			if (status != null)
	        {
				String statusT = status.getTextContent();
				if(statusT.equals("1")){
					Element walkscore = (Element)wsDoc.getElementsByTagName("walkscore").item(0);
					if (walkscore != null)
			        {
						walkscoreT = walkscore.getTextContent();
			        }
					Element wsdescription = (Element)wsDoc.getElementsByTagName("description").item(0);
					if (wsdescription != null)
			        {
						wsdesT = wsdescription.getTextContent();
			        }
					System.out.println("WalkScore for "+address+"  "+walkscoreT);
					System.out.println("Description  " +wsdesT);
					
					result = "  "+walkscoreT +","+ wsdesT;
				}
				else 
				{
					System.out.println("WalkScore isn't availible");
				result = "WalkScore isn't availible";
				}
				
	        }
			return result;
		
		}
}
